package com.example.tnot.Teacher.CoursePart.CourseInternal.CourseDetailHome.Statistics.StudyTime.Adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.tnot.R;
import com.example.tnot.Teacher.CoursePart.CourseInternal.CourseDetailHome.Statistics.StudyTime.ItemBean.StudyTime;

import java.time.Duration;
import java.util.List;

public class rvStudyTimeAdapter extends RecyclerView.Adapter<rvStudyTimeAdapter.ViewHolder> {
    private List<StudyTime> StudyData;

    public rvStudyTimeAdapter(List<StudyTime> studyTime) {
        this.StudyData = studyTime;
    }

    @NonNull
    @Override
    public rvStudyTimeAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_study_time,parent,false);
        return new ViewHolder((ViewGroup) view);
    }

    @Override
    public void onBindViewHolder(@NonNull rvStudyTimeAdapter.ViewHolder holder, int position) {
        StudyTime item = StudyData.get(position);
        //将学习数据默认隐藏
        holder.ll_studyData.setVisibility(View.GONE);

        // 设置基础数据
        holder.tv_name.setText(item.getName());
        holder.iv_icon.setImageResource(item.getIcon());
        holder.iv_studyTime.setImageResource(item.getStudyTimePhoto());

        // 根据数据状态设置初始视图状态
        if (item.isClick()) {
            holder.ll_studyData.setVisibility(View.VISIBLE);
            holder.iv_arrow.setImageResource(R.drawable.pull_down);
        } else {
            holder.ll_studyData.setVisibility(View.GONE);
            holder.iv_arrow.setImageResource(R.drawable.pull_up);
        }


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 更新数据源状态
                item.setClick(!item.isClick());
                // 更新视图状态
                if (item.isClick()) {
                    holder.ll_studyData.setVisibility(View.VISIBLE);
                    holder.iv_arrow.setImageResource(R.drawable.pull_down);
                } else {
                    holder.ll_studyData.setVisibility(View.GONE);
                    holder.iv_arrow.setImageResource(R.drawable.pull_up);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return StudyData.size();
    }
    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView tv_name;
        ImageView iv_icon,iv_studyTime,iv_arrow;
        LinearLayout ll_studyData;

        public ViewHolder(@NonNull ViewGroup itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
            iv_icon = itemView.findViewById(R.id.iv_icon);
            iv_arrow = itemView.findViewById(R.id.iv_arrow);
            ll_studyData = itemView.findViewById(R.id.ll_studyData);
            iv_studyTime = itemView.findViewById(R.id.iv_studyTime);
        }
    }
}
